package main

import (
	"encoding/json"
	"fmt"
	"strings"
	"time"
	_ "zhoubao/routers"

	"gitee.com/konyshe/gogo"
)

type WeeklyStruct struct {
	ID         string `json:"id"`
	ProjectID  string `json:"project_id"`
	WeekNum    string `json:"weeknum"`
	UpdateTime string `json:"updatetime"`
	Body       string `json:"body"`
	IP         string `json:"ip"`
	View       string `json:"view"`
}

type JSONStruct struct {
	Action        string      `json:"action"`
	ProjectID     string      `json:"project_id"`
	WeekNumOfYear string      `json:"weeknum_of_year"`
	JSONValue     interface{} `json:"json_value"`
}

func GetTableWeekly(Action, ProjectID string) ([]byte, error) {
	queryData, err := gogo.SQLQueryByMap(
		"", "*", "weekly", "view=1 and project_id="+ProjectID, "-weeknum", 0, 20)

	if err == nil {
		var sendJSON JSONStruct
		sendJSON.Action = Action
		sendJSON.JSONValue = queryData

		return json.Marshal(sendJSON)
	}

	return nil, nil
}

func webSocketHandle(data []byte, conn *gogo.ImplConnection) ([]byte, error) {
	var recvJSON JSONStruct
	err := json.Unmarshal(data, &recvJSON)
	if err != nil {
		return nil, err
	}

	switch recvJSON.Action {
	case "get_table_project":
		{
			queryData, err := gogo.SQLQueryByMap(
				"", "*", "project", "view=1", "+sort", 0, 100)

			if err == nil {
				var sendJSON JSONStruct
				sendJSON.Action = "get_table_project"
				sendJSON.JSONValue = queryData

				return json.Marshal(sendJSON)
			}

			return nil, err
		}

	case "get_table_weekly":
		{
			if recvJSON.ProjectID == "" {
				return nil, nil
			}
			return GetTableWeekly("get_table_weekly", recvJSON.ProjectID)
		}

	case "get_ipuser":
		{
			queryData, err := gogo.SQLQueryByMap(
				"", "*", "ipuser", "", "+id", 0, 100)

			if err == nil {
				var sendJSON JSONStruct
				sendJSON.Action = "get_ipuser"
				sendJSON.JSONValue = queryData

				return json.Marshal(sendJSON)
			}

			return nil, err
		}

	case "get_max_weeklynum":
		{
			queryData, err := gogo.SQLQueryByMap(
				"", "max(weeknum)", "weekly", "view=1 and project_id="+recvJSON.ProjectID, "", 0, 1)

			if err == nil {
				var sendJSON JSONStruct
				sendJSON.Action = "get_max_weeklynum"
				sendJSON.ProjectID = recvJSON.ProjectID
				sendJSON.JSONValue = queryData

				return json.Marshal(sendJSON)
			}

			return nil, nil
		}

	case "get_weekly_count":
		{
			queryData, err := gogo.SQLQueryByMap(
				"", "count(*)", "weekly", "view=1 and project_id="+recvJSON.ProjectID, "", 0, 1)

			if err == nil {
				var sendJSON JSONStruct
				sendJSON.Action = "get_weekly_count"
				sendJSON.ProjectID = recvJSON.ProjectID
				sendJSON.JSONValue = queryData

				return json.Marshal(sendJSON)
			}

			return nil, err
		}

	case "up_weekly":
		{
			b, err := json.Marshal(recvJSON.JSONValue)
			if err != nil {
				return nil, err
			}

			var RecvWeekly WeeklyStruct
			err = json.Unmarshal(b, &RecvWeekly)
			if err != nil {
				return nil, err
			}

			var WeeklyMap map[string]string
			WeeklyMap = make(map[string]string)
			WeeklyMap["body"] = RecvWeekly.Body

			WeeklyByte, err := json.Marshal(WeeklyMap)

			_, err = gogo.SQLUpdate(
				"weekly",
				"id="+RecvWeekly.ID,
				WeeklyByte)

			if err == nil {
				return GetTableWeekly("up_weekly", RecvWeekly.ProjectID)
			}

			return nil, err
		}

	case "add_weekly":
		{
			b, err := json.Marshal(recvJSON.JSONValue)
			if err != nil {
				return nil, err
			}

			var RecvWeekly WeeklyStruct
			err = json.Unmarshal(b, &RecvWeekly)
			if err != nil {
				return nil, err
			}

			t := time.Now()

			var Weekly map[string]string
			Weekly = make(map[string]string)
			Weekly["project_id"] = RecvWeekly.ProjectID
			Weekly["weeknum"] = RecvWeekly.WeekNum
			Weekly["updatetime"] = fmt.Sprintf("%d-%d-%d %d:%d:%d\n", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
			Weekly["body"] = RecvWeekly.Body
			Weekly["ip"] = strings.Split(conn.RemoteAddr().String(), ":")[0]
			Weekly["view"] = "1"

			WeeklyList := []map[string]string{Weekly}

			WeeklyListByte, err := json.Marshal(WeeklyList)

			if _, err = gogo.SQLInsert("weekly", WeeklyListByte); err == nil {
				return GetTableWeekly("add_weekly", RecvWeekly.ProjectID)
			}

			return nil, err
		}

	case "get_sysconfig":
		{
			queryData, err := gogo.SQLQueryByMap(
				"", "*", "sysconfig", "", "+id", 0, 20)

			if err == nil {
				var sendJSON JSONStruct
				sendJSON.Action = "get_sysconfig"
				sendJSON.JSONValue = queryData

				return json.Marshal(sendJSON)
			}

			return nil, err

			/*t := time.Now()
			dt1 := time.Date(2019, 1, 1, 0, 0, 0, 0, time.Local)
			dt2 := time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), time.Local) // 不用关注时区，go会转换成时间戳进行计算
			weeknum := dt2.Sub(dt1).Hours() / 24 / 7

			if weeknum >= 11.43 {
				weeknum++
			}

			var sendJSON JsonStruct
			sendJSON.Action = "get_weeknum_of_year"
			sendJSON.WeekNumOfYear = fmt.Sprintf("%.0f", weeknum)

			return json.Marshal(sendJSON)*/
		}
	}

	return nil, nil
}
